################################################################################
# Created By:Pietryka
# Creation Date:  2016-08-22
# Purpose: Create Figure A3. Sensitivity analysis for the nonvoting participation
# Questions: mpietryka@fsu.edu
################################################################################


# PREAMBLE  ====================================


# LOAD PACKAGES --------------

library(tidyverse)


# LOAD DATA  --------------

# Workspace from "Data/CPS-4B-Matching-Participation.R"
load("Data/CPS-4B-Matching-Participation.Rdata")

# sensitivity function
source("../Functions/sensitivity_ci.R")



#  SENSITIVITY ESTIMATES =================================


# VALUES FOR BIAS
gamma_pos <- seq(-0.1, 0.1, 0.02)

# LABELS
label_1 <- "Assumes no bias"
label_neg <- "Assumes parents LESS likely\nto vote than non-parents"
label_pos <- "Assumes parents MORE likely\nto vote than non-parents"

# COLORS
color_1 <- "#2DE1FC"
color_2 <- "grey85"
color_insig <- "white"
color_annotate <- "#646881"


# COMBINE RESULTS
results10_df <- data_frame(y = outcome_names,
                           mod = map(cps_nonvoting_est_10, "att.model"))  %>%
  mutate(beta = map_dbl(mod, ~ .x["Estimate", "prekkids"]))  %>%
  mutate(se   = map_dbl(mod, ~ .x["Std. Error", "prekkids"]))   %>%
  mutate(lb   = beta - 1.96 * se)   %>%
  mutate(ub   = beta + 1.96 * se)    %>%
  select(-mod)  %>%
  mutate(outcome_lab = c(
    "Discuss Politics",
    "Contact Official",
    "Community Group",
    "Civic Group",
    "Serve as Officer"
  ))



sens10_df <-   results10_df  %>%
  mutate(sens_2 = map2(beta, se, ~
                         s_ci(.x, .y,
                              gamma = gamma_pos,
                              delta = 0.2,
                              mod_type = "lm")))  %>%
  mutate(sens_4 = map2(beta, se, ~
                         s_ci(.x, .y,
                              gamma = gamma_pos,
                              delta = 0.4,
                              mod_type = "lm")))  %>%
  mutate(sens_6 = map2(beta, se, ~
                         s_ci(.x, .y,
                            gamma = gamma_pos,
                            delta = 0.6,
                            mod_type = "lm")))  %>%
  gather(delta, sens, starts_with("sens_"))  %>%
  mutate(delta = as.numeric(gsub("sens_", "", delta))/10)  %>%
  mutate(delta = paste("delta==", delta))  %>%
  mutate(gamma = map(sens, "gamma"))  %>%
  mutate(beta_crt = map(sens, "corrected_beta"))  %>%
  mutate(lb_crt = map(sens, "corrected_lb"))  %>%
  mutate(ub_crt = map(sens, "corrected_ub"))  %>%
  select(-sens)  %>%
  unnest()   %>%
  mutate(dot_type = case_when(
    beta == beta_crt ~ "original",
    sign(lb_crt) != sign(ub_crt) ~ "statistically insignificant",
    beta != beta_crt ~ "corrected"
  ))  %>%
  mutate(line_type = case_when(
    beta == beta_crt ~ "original",
    beta != beta_crt ~ "corrected"
  ))  %>%
  mutate(similarity = beta - beta_crt)






# SENSITIVITY PLOT =============================================


df <- sens10_df
the_plot <- ggplot(df, aes(
  x = gamma,
  y = beta_crt,
  color = line_type,
  fill = dot_type
)) +
  geom_hline(yintercept = 0,
             linetype = 2,
             linewidth = 1) +
  geom_pointrange(
    aes(ymin = lb_crt, ymax = ub_crt),
    shape = 21,
    alpha = 1,
    fatten = 4,
    linewidth = 1.1
  ) +
  facet_grid(outcome_lab ~ delta, scales = "free_y",
             labeller = labeller(.cols = label_parsed)) +
  xlab(expression(gamma)) +
  ylab("Effect of Parenthood on non-voting Participation") +
  labs(title = NULL) +
  expand_limits(x = c(-0.12, 0.12)) +
  theme_minimal(base_size = 12) +
  theme(plot.caption = element_text(colour = color_annotate)) +
  theme(legend.position = "none") +
  theme(panel.grid.minor = element_blank()) +
  scale_color_manual(values = c("grey66", "black")) +
  scale_fill_manual(values = c("grey66", "black", "white"))




graphics.off()
windows(6.5, 8)
the_plot

# save -------------
ggsave("Figures/CPS-Participation-Sensitivity.tiff", compression = "lzw")
